﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace danalyser.Interface.Dialogs
{
    using Controllers;

    /// <summary>
    /// Окно настроек статических анализаторов
    /// </summary>
    public partial class AnalysersSettingsForm : Form
    {
        // Настройки изменены
        private bool isChanged = false;

        private void ReCheck()
        {
            foreach (DataGridViewRow row in errorSBox.Rows)
                row.Cells[1].Value = AnalysisController.Instance.Analysers[row.Index].Enabled;
        }

        private new void Load()
        {
            AnalysisController.Instance.LoadAnalysers();
            ReCheck();
            isChanged = false;
        }

        private void Save()
        {
            AnalysisController.Instance.SaveAnalysers();
            ReCheck();
            isChanged = false;
        }

        public void PropertiesChanged(object sender, EventArgs e)
        {
            isChanged = true;
            analyserProperties.Refresh();
            ReCheck();
        }

        private void Init()
        {
            int i, l = AnalysisController.Instance.Analysers.Count;
            for (i = 0; i < l; i++)
            {
                errorSBox.Rows.Add(AnalysisController.Instance.Analysers[i].Name, AnalysisController.Instance.Analysers[i].Enabled);
                AnalysisController.Instance.Analysers[i].PropertiesChangedEvent += new EventHandler<EventArgs>(PropertiesChanged);
            }
        }
        
        public AnalysersSettingsForm()
        {
            InitializeComponent();
            Init();
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            Save();
            Close();
        }

        private void exitBtn_Click(object sender, EventArgs e)
        {
            Load();
            Close();
        }

        private void AnalysersSettingsForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChanged)
            {
                switch (MessageBox.Show("Хотите сохранить изменения?", "Внимание", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Asterisk))
                {
                    case DialogResult.Yes:
                        Save();
                        break;
                    case DialogResult.No:
                        Load();
                        break;
                    case DialogResult.Cancel:
                        e.Cancel = true;
                        break;
                }
            }
        }

        private void errorSBox_SelectionChanged(object sender, EventArgs e)
        {
            if (errorSBox.SelectedRows.Count > 0)
                analyserProperties.SelectedObject = AnalysisController.Instance.Analysers[errorSBox.SelectedRows[0].Index];
        }

        private void errorSBox_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if ((e.RowIndex > -1) && (AnalysisController.Instance.Analysers[e.RowIndex].Enabled != (bool)errorSBox.Rows[e.RowIndex].Cells[1].Value))
            {
                AnalysisController.Instance.Analysers[e.RowIndex].Enabled = (bool)errorSBox.Rows[e.RowIndex].Cells[1].Value;
                analyserProperties.Refresh();
            }
        }

        private void errorSBox_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            bool before = (bool)errorSBox.Rows[e.RowIndex].Cells[1].Value;
            errorSBox.EndEdit();
            bool after = (bool)errorSBox.Rows[e.RowIndex].Cells[1].Value;
            if (before ^ after)
            {
                isChanged = true;
                AnalysisController.Instance.Analysers[e.RowIndex].Enabled = after;
                analyserProperties.Refresh();
            }
        }

        public new void ShowDialog()
        {
            ReCheck();
            base.ShowDialog();
        }

        private void AnalysersSettingsForm_Load(object sender, EventArgs e)
        {

        }
    }
}
